package com.yzcs.tvapp

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.fy.baselibrary.aop.annotation.StatusBar
import com.fy.baselibrary.application.mvvm.BaseViewModel
import com.fy.baselibrary.application.mvvm.IBaseMVVM
import com.fy.baselibrary.retrofit.RxHelper
import com.fy.baselibrary.retrofit.observer.RequestBaseObserver
import com.fy.baselibrary.statusbar.StatusBarContentColor
import com.fy.baselibrary.utils.JumpUtils
import com.fy.baselibrary.utils.ResUtils
import com.yzcs.tvapp.databinding.SplashActivityBinding
import com.yzcs.tvapp.simple.activity.RecommendVideoActivity
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

/**
 * description 启动页
 * Created by fangs on 2021/1/18 17:28.
 */
class SplashActivity : AppCompatActivity(), IBaseMVVM<BaseViewModel, SplashActivityBinding> {

    lateinit var vdb: SplashActivityBinding

    override fun setContentLayout(): Int {
        return R.layout.splash_activity
    }

    @StatusBar(statusColor = R.color.appBg, navColor = R.color.txtLight)
    override fun initData(viewModel: BaseViewModel?,
                          dataBinding: SplashActivityBinding?,
                          savedInstanceState: Bundle?) {
        StatusBarContentColor.setStatusTextColor(this, true, false)

        vdb = dataBinding!!
        startTimer()
    }

    private val skip = 3L
    private fun startTimer() {
        vdb.tvSkip.text = ResUtils.getReplaceStr(R.string.skip, skip)

        Observable.interval(0, 1, TimeUnit.SECONDS)
            .take(skip + 1)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxHelper.bindToLifecycle(this))
            .subscribe(object : RequestBaseObserver<Long>() {
                override fun onSuccess(data: Long) {
                    vdb.tvSkip.text = ResUtils.getReplaceStr(R.string.skip, skip - data)
                }

                override fun onComplete() {
                    super.onComplete()
                    JumpUtils.jump(this@SplashActivity, RecommendVideoActivity::class.java, null)
                    finish()
                }
            })
    }

}